package io.terminus.spzx.manager.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.terminus.spzx.common.log.annotation.SpzxLog;
import io.terminus.spzx.manager.service.SysRoleService;
import io.terminus.spzx.model.dto.system.SysRoleDto;
import io.terminus.spzx.model.entity.system.SysRole;
import io.terminus.spzx.model.vo.common.Result;
import io.terminus.spzx.model.vo.common.ResultCodeEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author lichunlong lcl253137@alibaba-inc.com
 * @Desc 系统角色Controller
 * @Date 2024-05-09
 */
@RestController
@RequiredArgsConstructor
@Tag(name = "系统角色接口")
@RequestMapping(value = "/admin/system/role")
public class SysRoleController {

    private final SysRoleService sysRoleService;

    /**
     * 分页查询角色列表
     *
     * @param sysRoleDto 角色查询条件
     * @param current    当前页
     * @param limit      每页记录数
     * @return 角色列表
     */
    @PostMapping("/findByPage/{current}/{limit}")
    public Result<PageInfo<SysRole>> findByPage(@RequestBody SysRoleDto sysRoleDto, @PathVariable("current") Integer current, @PathVariable("limit") Integer limit) {
        PageInfo<SysRole> rolePageInfo = sysRoleService.findByPage(sysRoleDto, current, limit);

        return Result.build(rolePageInfo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据用户ID查询角色列表
     *
     * @param userId 用户ID
     * @return 返回结果<角色列表>
     */
    @GetMapping("/findByUserId/{userId}")
    public Result<Map<String, Object>> findByUserId(@PathVariable("userId") Long userId) {
        Map<String, Object> roleMap = sysRoleService.findByUserId(userId);

        return Result.build(roleMap, ResultCodeEnum.SUCCESS);
    }

    /**
     * 保存角色, 添加Log注解，设置属性
     *
     * @param sysRole 系统角色
     * @return 返回结果
     */
    @PostMapping("/saveSysRole")
    @SpzxLog(title = "角色添加", businessType = 1)
    public Result<Void> saveSysRole(@RequestBody SysRole sysRole) {
        sysRoleService.saveSysRole(sysRole);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 修改角色
     *
     * @param sysRole 系统角色
     * @return 返回结果
     */
    @PutMapping("/updateSysRole")
    public Result<Void> updateSysRole(@RequestBody SysRole sysRole) {
        sysRoleService.updateSysRole(sysRole);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 删除角色
     *
     * @param roleId 角色ID
     * @return 返回结果
     */
    @DeleteMapping("/deleteById/{roleId}")
    public Result<Void> deleteById(@PathVariable("roleId") Long roleId) {
        sysRoleService.deleteById(roleId);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}
